//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FrameProviderRefundDlg.h"
#include "FormUtilities.h"
#include "Calendar.h"
#include "StringUtilities.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

const int CODE_DATE        = 1;
const int CODE_AMOUNT      = 2;

//---------------------------------------------------------------------------

__fastcall TfrmFrameProviderRefundDlg::TfrmFrameProviderRefundDlg(TComponent* Owner, TFrameProviderBridge* bridge, TFrameRefund* refund)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->bridge = bridge;
   this->refund = refund;
   lblCurrency->Caption = "(" + CurrencyString + ")";

   Caption = "Update refund";
   original = new TFrameRefund( refund);
   ShowRefund();
}

__fastcall TfrmFrameProviderRefundDlg::TfrmFrameProviderRefundDlg(TComponent* Owner, TFrameProviderBridge* bridge, __int64 frameProviderID)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->bridge  = bridge;
   this->frameProviderID = frameProviderID;
   this->refund = NULL;
   lblCurrency->Caption = "(" + CurrencyString + ")";

   Caption = "Add new refund";
   original = NULL;
}

void TfrmFrameProviderRefundDlg :: ShowRefund()
{
   edtDate->Text   = DateToStr( refund->refundDate);
   edtAmount->Text = FloatToStr( refund->amount);
}

void TfrmFrameProviderRefundDlg :: GatherRefund()
{
   refund->refundDate = StrToDate( edtDate->Text);
   refund->amount     = edtAmount->Text;
}

void TfrmFrameProviderRefundDlg :: AddNewRefund()
{
   refund = new TFrameRefund();

   try
   {
      GatherRefund();
      refund->frameProviderID = frameProviderID;
      bridge->AddRefund( refund);
   }
   __finally
   {
      delete refund;
      refund = NULL;
   }//try-finally
}

void TfrmFrameProviderRefundDlg :: UpdateRefund()
{
   GatherRefund();
   bridge->UpdateRefund( original->id, refund);
}

void TfrmFrameProviderRefundDlg :: ValidateData()
{
   if ( edtDate->Text.Length() == 0)
      throw new TError( CODE_DATE, "Date must be entered");

   try
   {
      TDateUtilities::validate( edtDate->Text, "Illegal date");
   }
   catch( Exception * e)
   {
      throw new TError( CODE_DATE, e->Message);
   }//try-catch
   
   if ( edtAmount->Text.Length() == 0)
      throw new TError( CODE_AMOUNT, "Amount must be entered");

   try
   {
      StrToFloat( edtAmount->Text);
   }
   catch( Exception * e)
   {
      throw new TError( CODE_AMOUNT, "Illegal amount");
   }//try-catch
}

void TfrmFrameProviderRefundDlg :: HandleError( TError * e)
{
   MessageDlg( e->Message,
               mtError,
               TMsgDlgButtons() << mbOK,
               0);

   switch ( e->GetCode())
   {
      case CODE_DATE :
         edtDate->SetFocus();
         break;

      case CODE_AMOUNT :
         edtAmount->SetFocus();
         break;
   }//switch

   ModalResult = mrNone;
}

//---------------------------------------------------------------------------

void __fastcall TfrmFrameProviderRefundDlg::btnOKClick(TObject *Sender)
{
   try
   {
      ValidateData();
      
      if ( refund == NULL)
         AddNewRefund();
      else
         UpdateRefund();
   }
   catch( TError * e)
   {
      HandleError( e);
   }//try-catch
}
//---------------------------------------------------------------------------


void __fastcall TfrmFrameProviderRefundDlg::btnDateClick(TObject *Sender)
{
   TfrmCalendar * frmCalendar;

   frmCalendar = new TfrmCalendar( this);

   try
   {
      if ( edtDate->Text.Length() > 0)
         frmCalendar->calendar->Date = StrToDate( edtDate->Text);

      if ( frmCalendar->ShowModal() == mrOk)
         edtDate->Text = DateToStr( frmCalendar->calendar->Date);
   }
   __finally
   {
      delete frmCalendar;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmFrameProviderRefundDlg::FormDestroy(TObject *Sender)
{
   if ( original != NULL)
      delete original;
}
//---------------------------------------------------------------------------

